home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Multimedia & Desktop / sk8 / SK8InJava / Code / Renderers / Gradient.java < prev    next >
Encoding:
Java Source  |  1997-02-27  |  4.4 KB  |  175 lines  |  [TEXT/CWIE]

  1. /*  SK8 © 1997 Apple Computer, Inc.
  2.     This code is protected under the current SK8 License
  3.     See http://sk8.research.apple.com/ for more information
  4.     Apple Research Laboratories
  5. */
  6.  
  7.  
  8. import java.awt.*;
  9.  
  10. class gradient extends renderer {
  11.     //CONSTANTS FOR DIRECTION OF GRADIENT.
  12.     public static int horizontal = 0;
  13.     public static int vertical = 1;
  14.     public static int oval = 2;
  15.     public static int rect = 3;
  16.     
  17.     //PROPERTIES AND THEIR GETTERS AND SETTERS.
  18.     int startredVar;
  19.     int startgreenVar;
  20.     int startblueVar;
  21.     int endredVar;
  22.     int endgreenVar;
  23.     int endblueVar;
  24.     int directionVar;
  25.     
  26.     public int startred() { return startredVar;}
  27.     public void setstartred(int val) { startredVar = val; }
  28.     
  29.     public int startgreen() { return startgreenVar;}
  30.     public void setstartgreen(int val) { startgreenVar = val; }
  31.     
  32.     public int startblue() { return startblueVar;}
  33.     public void setstartblue(int val) { startblueVar = val; }
  34.  
  35.     public int endred() { return endredVar;}
  36.     public void setendred(int val) { endredVar = val; }
  37.     
  38.     public int endgreen() { return endgreenVar;}
  39.     public void setendgreen(int val) { endgreenVar = val; }
  40.     
  41.     public int endblue() { return endblueVar;}
  42.     public void setendblue(int val) { endblueVar = val; }
  43.  
  44.     public int direction() { return directionVar;}
  45.     public void setdirection(int val) { 
  46.         if ((val < 0) || (val > 3)) 
  47.             directionVar = 0;
  48.         else
  49.             directionVar = val; }
  50.  
  51.     
  52.     //CONSTRUCTORS
  53.     public gradient() {
  54.         this(0,0,0,255,255,255,0);
  55.     }
  56.     
  57.     public gradient(int sred, int sgreen, int sblue,
  58.                     int ered, int egreen, int eblue,
  59.                     int dir) {
  60.         this.setstartred(sred);
  61.         this.setstartgreen(sgreen);
  62.         this.setstartblue(sblue);
  63.         this.setendred(ered);
  64.         this.setendgreen(egreen);
  65.         this.setendblue(eblue);
  66.         this.setdirection(dir);
  67.     }
  68.     
  69.     //THE RENDER METHOD
  70.     void render(Graphics g, actor act, Region reg) {
  71.         Color currentColor;
  72.         Rectangle drawrect = reg.getBBox();
  73.         Rectangle bounds = act.boundsrect();
  74.         float steps = 1;
  75.         switch (directionVar) {
  76.             case 0: steps = bounds.width;
  77.                     break;
  78.             case 1: steps = bounds.height;
  79.                     break;
  80.             case 2: if (bounds.width > bounds.height)
  81.                         steps = bounds.width;
  82.                     else
  83.                         steps = bounds.height;
  84.                     break;
  85.             case 3: if (bounds.width > bounds.height)
  86.                         steps = bounds.width;
  87.                     else
  88.                         steps = bounds.height;
  89.                     break;
  90.         }
  91.         float curRed = startredVar;
  92.         float curGreen = startgreenVar;
  93.         float curBlue = startblueVar;
  94.         
  95.         float stepRed = (endredVar - startredVar) / steps;
  96.         float stepGreen = (endgreenVar - startgreenVar) / steps;
  97.         float stepBlue = (endblueVar - startblueVar) / steps;
  98.         int x1,x2,y1,y2;
  99.         int i;
  100.         switch (directionVar) {
  101.             case 0:
  102.             y1 = bounds.y;
  103.             y2 = bounds.height;
  104.             x1 = bounds.x;
  105.             for (i = 0; i < steps; i++) {
  106.                 currentColor = new Color (Math.round(curRed), 
  107.                                         Math.round(curGreen),
  108.                                         Math.round(curBlue));
  109.                 g.setColor(currentColor);
  110.                 g.fillRect(x1,y1,1,y2);
  111.                 curRed += stepRed;
  112.                 curGreen += stepGreen;
  113.                 curBlue += stepBlue;
  114.                 x1++; }
  115.                 break;
  116.  
  117.             case 1:
  118.             x1 = bounds.x;
  119.             x2 = bounds.width;
  120.             y1 = bounds.y;
  121.             for (i = 0; i < steps; i++) {
  122.                 currentColor = new Color (Math.round(curRed), 
  123.                                         Math.round(curGreen),
  124.                                         Math.round(curBlue));
  125.                 g.setColor(currentColor);
  126.                 g.fillRect(x1,y1,x2,1);
  127.                 curRed += stepRed;
  128.                 curGreen += stepGreen;
  129.                 curBlue += stepBlue;
  130.                 y1++;}
  131.                 break;
  132.             case 2:
  133.                 x1 = bounds.x;
  134.                 y1 = bounds.y;
  135.                 x2 = bounds.width;
  136.                 y2 = bounds.height;
  137.                 g.setColor(new Color (Math.round(curRed), 
  138.                                                     Math.round(curGreen),
  139.                                                     Math.round(curBlue)));
  140.                 g.fillRect(x1,y1,drawrect.width,drawrect.height);
  141.                 for (i = 0; i < steps; i++) {
  142.                     g.setColor(new Color (Math.round(curRed), 
  143.                                           Math.round(curGreen),
  144.                                           Math.round(curBlue)));
  145.                     g.fillOval(x1 + i,y1 + i, 
  146.                                 (x2 - i) - i, (y2 - i) - i);
  147.                     curRed += stepRed;
  148.                     curGreen += stepGreen;
  149.                     curBlue += stepBlue;}
  150.                 break;
  151.             case 3:
  152.                 x1 = bounds.x;
  153.                 y1 = bounds.y;
  154.                 x2 = bounds.width;
  155.                 y2 = bounds.height;
  156.                 g.setColor(new Color (Math.round(curRed), 
  157.                                         Math.round(curGreen),
  158.                                         Math.round(curBlue)));
  159.                 g.fillRect(x1,y1,drawrect.width,drawrect.height);
  160.                 for (i = 0; i < steps; i++) {
  161.                     g.setColor(new Color (Math.round(curRed), 
  162.                                             Math.round(curGreen),
  163.                                             Math.round(curBlue)));
  164.                     g.fillRect(x1 + i,y1 + i, 
  165.                                 (x2 - i) - i, (y2 - i) - i);
  166.                     curRed += stepRed;
  167.                     curGreen += stepGreen;
  168.                     curBlue += stepBlue;}
  169.                 break;
  170.             }
  171.     }
  172.  
  173.  
  174. }
  175.